from config import *
import xmb
import gys


class FBS:
    def __init__(self, name, warehouse, price_out):
        self.name = name
        self.warehouse = warehouse  # 分包商的仓库名字
        self.price_out = price_out  # 分包商出库单价

        self.application_form = ""  # 物资申请单
        self.put_in_form = ""  # 分包商入库单

        # self.application_form = "SQ-20240502-8195"  # 物资申请单

    # 切换角色
    def into(self):
        sleep(1)
        xpath("//span[contains(text(), '好')]")  # 点击 右上角的 “上午好或下午好”
        xpath("//li[contains(text(), '切换账号')]")  # 点击 切换账号
        xpath("//div[contains(text(), '%s')]" % self.name)  # 点击 名字 进行切换 账号
        sleep(2)

    def add_application_form(self):
        sleep(2)
        # 新增物质申请单
        query('//*[@id="app"]/div/section/section/main/section/div/*/form/*', "申请单")
        # 项目标段
        sleep(1)
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[2]/div/div/div/div[1]/input')
        sleep(0.5)
        query('/html/body/*/*/*/ul/li', project_section)  # 标段位置
        # 关联项目
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[5]/div/div/div/div[1]/div[1]/input')
        # 项目名称 （人工挖运土方 ）
        xpath(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[5]/div/div/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div[4]/div[1]/label/span/span')
        xpath_input(
            '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/form/div/div[6]/div/div/div/textarea',
            '测试')  # 备注
        # 新增加申请单的物料信息
        material_message(material_name, count,
                         '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/div[1]/div/div[3]/table/tbody/tr/td[2]/div/div/input',
                         '//*[@id="app"]/div/section/section/main/section/div/div[5]/div/div[2]/div/div[1]/form/div[2]/div/div/input',
                         '//*[@id="app"]/div/section/section/main/section/div/div[5]/div/div[2]/div/div[1]/form/button',
                         '//*[@id="app"]/div/section/section/main/section/div/div[5]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[1]/div/button',
                         '//*[@id="app"]/div/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/div[1]/div/div[3]/table/tbody/tr/td[6]/div/div/input')
        # 提交物料申请单
        sleep(0.2)
        query('//button[2]',"提交单据") # 报错一次
        sleep(1)
        # 保存 物资申请单单号
        self.application_form = xpath_text(
            '//*[@id="app"]/div/section/section/main/section/div/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[2]/div')
        print('物质申请单：' + self.application_form)
        sleep(1)

    # 分包商出库施工单位 暂时只支持施工单位
    def put_out(self):
        sleep(2)  # 报错 地方 找不到 出库右上 按钮 强制停止
        query("//label[1]", "出库")
        query('//*[@id="app"]/div/section/section/main/section/div/*/*/*/*/*/*/*/*', "新增出库单")
        # 出库对象
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div/input')
        query('/html/body/*/*/*/ul/*', "施工部位")
        # 标段项目
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[3]/div[2]/div/div/input')
        sleep(0.5)
        query('/html/body/*/*/*/ul/*', project_section)
        # 出库仓库
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div[1]/div[2]/div/div[1]/div[4]/div[2]/div/div/input')
        query('/html/body/*/*/*/ul/*', self.warehouse)
        # 分段工程
        xpath(
            "/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[5]/div[2]/div/div[1]/div/input")
        xpath(
            "/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[5]/div[2]/div/div[2]/div/div[3]/div[2]/div[2]/div[2]/div[2]/div[1]/label/span/span")
        # 关联入库单
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[6]/div[2]/div/div[1]/input')
        query('/html/body/*/*/*/ul/*', self.put_in_form)
        # 收货地址、备注
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[9]/div[2]/div/input',
            location)
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[10]/div[2]/div/textarea',
            "备注默认")
        # 运输信息 、 签收信息
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[11]/div[5]/div[2]/div/label[2]/span[1]/span')
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[11]/div[2]/div[2]/div/input',
            "粤A-88888")
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[11]/div[3]/div[2]/div/input',
            "炸薯条")
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[11]/div[4]/div[2]/div/input',
            "123456789")
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[11]/div[5]/div[2]/div/label[2]/span[1]/span')
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[11]/div[6]/div[2]/div/input',
            "烤薯条")
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[1]/div[11]/div[7]/div[2]/div/input')
        query('/html/body/*/*/*', "此刻")  # 签收时间
        # 出库数量和单价
        driver.find_element_by_xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[8]/div/div/input').clear()  # 清空输入框默认信息
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[8]/div/div/input',
            self.price_out)  # 输入信息
        driver.find_element_by_xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[9]/div/div/input').clear()
        xpath_input(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[2]/div/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[9]/div/div/input',
            count)
        sleep(0.5)
        query('//*[@id="app"]/div/section/section/main/section/div/*/*/*/*/*/*', "确定出库");sleep(2)
        xpath('/html/body/div[1]/div/section/section/main/section/div/div[6]/div[1]/div[1]/div[1]/button/i') # 右上角的x

    def put_in(self):  # 入库
        xpath('//*[@id="app"]/div/section/section/main/section/div/*/ul/*/div[contains(text(), "出入库管理")]')
        sleep(0.5)
        query("//label[2]", "入库")  # 右上角
        query('/html/body/div[1]/div/section/section/main/section/div/*/*/div/*/div/*/div/*/table/tbody/*/*/div/*',
              "处理")
        # 保存单号
        self.put_in_form = xpath_text(
            '/html/body/div[1]/div/section/section/main/section/div/*/div[1]/div/div[2]/div/div[3]/div/div[3]/table/tbody/tr[1]/td[2]/div')
        # 入库仓库
        xpath(
            '/html/body/div[1]/div/section/section/main/section/div/div[6]/div[3]/div/div[2]/div/div[1]/div[1]/div[5]/div[2]/div/div/input')
        query('/html/body/*/*/*/ul/*', self.warehouse)
        print("分包商入库单：" + self.put_in_form)
        sleep(1)  # 一次保存 无法点击签收入库 加时间 循环第六次错误
        query('/html/body/div[1]/div/section/section/main/section/div/*/*/div/*/div/*', "签收入库")
